package com.yiwei.zxp.view;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.PacketPayTypeAdapter;
import com.yiwei.zxp.api.model.PayWayListModel;
import com.yiwei.zxp.constant.UserInfoGlobal;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xsooy-pc on 18-5-22.
 */

public class PacketPayTypeDilaog extends Dialog {


    @BindView(R.id.btn_cancel)
    ImageView btnCancel;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.txt_info)
    TextView txtInfo;
    private Context context;
    private String select_pay;
    private PacketPayTypeAdapter adapter;
    //    PayWayListModel.DatasBean data;
    private String enName;
    private boolean isOver5000 = false;
    private String xianXia;

    public PacketPayTypeDilaog(Context context) {
        this(context, R.style.DialogStyle);

    }

    public PacketPayTypeDilaog setData(PayWayListModel data) {
//        this.data = data;
        adapter.update(data.datas);
        txtInfo.setText("温馨提示：每张银行卡单笔限额：" + UserInfoGlobal.getBankDayLimit() + "元/单日限额：" + UserInfoGlobal.getBankDayLimit() + "元/单月限额：" + UserInfoGlobal.getBankMonthLimit() + "元，请悉知！如果支付金额比较大，建议采用线下转账支付");
//        txtInfo.setText("温馨提示：每张银行卡单笔限额：" + data.bank_limit.day_limit + "元/单日限额：" + data.bank_limit.day_limit + "元/单月限额：" + data.bank_limit.month_limit + "元，请悉知！如果支付金额比较大，建议采用线下转账支付支付");
        return this;
    }

    public PacketPayTypeDilaog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_packet_pay_type);
        ButterKnife.bind(this);
        setCancelable(false);

        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = Util.getDisplayMetrics(context).widthPixels;
        lp.gravity = Gravity.BOTTOM;
        mWindow.setAttributes(lp);
        adapter = new PacketPayTypeAdapter(context);
        adapter.setOnChooseListener(new PacketPayTypeAdapter.OnChooseListener() {
            @Override
            public void onClick(String id, String en_name, String xianxia) {
                select_pay = id;
                enName = en_name;
                xianXia = xianxia;
            }
        });
        list.setAdapter(adapter);

    }

    public PacketPayTypeDilaog isOver5000(double isOver5000) {
        btnSubmit.setText("去支付" + isOver5000 + "元");
        if (isOver5000 > Double.valueOf(UserInfoGlobal.getBankDayLimit())) {
            this.isOver5000 = true;
        } else {
            this.isOver5000 = false;
        }
        return this;
    }

    @OnClick({R.id.btn_cancel, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;

            case R.id.btn_submit:
                if (TextUtils.isEmpty(select_pay)) {
                    ToastUtil.makeText(context, "请选择支付方式");
                    return;
                }
                if (isOver5000 && enName.equals("union")) {
                    com.yiwei.zxp.util.Dialog.showDefaultDialog(context, "", "单笔限额已超过5000.00元建议采用线下支付", "", "好，我知道了", false, new com.yiwei.zxp.util.Dialog.DialogDefaultClickListener() {
                        @Override
                        public void confirm() {

                        }

                        @Override
                        public void cancel() {

                        }
                    });
                    return;
                }
                if (onChooseListener != null) {
                    onChooseListener.onPayType(select_pay, enName, xianXia);
                }
                dismiss();
                break;
        }
    }

    private OnChooseListener onChooseListener;

    public PacketPayTypeDilaog setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
        return this;
    }

    public interface OnChooseListener {
        void onPayType(String selectPoi, String enName, String xianXia);
    }
}
